package com.laitaoo.service.home
{
	import com.laitaoo.event.HomeEvent;
	import com.laitaoo.event.StockListEvent;
	import com.laitaoo.service.BaseService;
	import com.maclema.mysql.MySqlResponse;
	import com.maclema.mysql.MySqlToken;
	import com.maclema.mysql.ResultSet;
	
	import mx.collections.ArrayCollection;
	import mx.rpc.AsyncResponder;
	
	[RouteEvents(events="getStockListByDayIdResult,getProductKindsResult,getStockDateListResult")]
	[Component]
	public class StockListService extends BaseService
	{
		private var _dayId:int;
		
		private var _kinds:ArrayCollection;
		
		public function StockListService()
		{
			super();
		}
		
		/**
		 * 获取每日的进货列表 
		 **/
		public function getStockList(dayId:int):void{
			
			_dayId = dayId;
			
			getKindList(kindResultHandler,kindsFaultHandler);
		}
		private function kindResultHandler(data:Object, token:Object):void{
			
			var rs:ResultSet = ResultSet(data);
			
			_kinds = rs.getRows();
			
			var sql:String = "SELECT temp.productName,temp.pId,temp.price,temp.totalCount,temp.totalPrice,temp.uint,temp.commont,temp.kindId,temp.stockId FROM (SELECT product.pId,product.productName,product.kindId,stocklist.price,stocklist.totalCount,stocklist.totalPrice,stocklist.uint,stocklist.commont,stocklist.productId,stocklist.dayId,stocklist.stockId FROM product LEFT JOIN stocklist ON product.pId=stocklist.productId and stocklist.dayId='"+_dayId+"') AS temp LEFT JOIN years ON temp.dayId=years.Id;";
			
			var tokens:MySqlToken = st.executeQuery(sql);
			
			tokens.addResponder(new AsyncResponder(stockListHandler,stockListFaultHandler,tokens));
		}
		private function kindsFaultHandler(info:Object, token:Object):void{
		}
		
		/** 获取进货列表失败 */
		private function stockListHandler(data:Object, token:Object):void{
			
			var rs:ResultSet = ResultSet(data);
			
			var rows:ArrayCollection = rs.getRows() as ArrayCollection;
			
			for each (var kindObj:Object in _kinds) 
			{
				kindObj.kinds = [];
				
				for each (var productObj:Object in rows) 
				{
					if(kindObj.Id == productObj.kindId){
						productObj.kindName = kindObj.kindName;
						(kindObj.kinds as Array).push(productObj);
					}
				}
			}
			
			var homeEvent:StockListEvent = new StockListEvent(StockListEvent.GETSTOCKLISTBYDAYIDRESULT);
			homeEvent.strockList = _kinds;
			dispatchEvent(homeEvent);
		}
		/** 获取进货列表失败 */
		private function stockListFaultHandler(info:Object, token:Object):void{
			
			
		}
		
		// ==============================================================================================================================
		
		public function getProductKinds():void{
			
			var sql:String = "select * from kinds";
			
			var token:MySqlToken = st.executeQuery(sql);
			
			token.addResponder(new AsyncResponder(productKindsHandler,productKindsFaultHandler,token));
		}
		/** 获取进货列表失败 */
		private function productKindsHandler(data:Object, token:Object):void{
			
			var rs:ResultSet = ResultSet(data);
			
			var rows:ArrayCollection = rs.getRows() as ArrayCollection;
			
			var homeEvent:HomeEvent = new HomeEvent(HomeEvent.GETPRODUCTKINDSRESULT);
			homeEvent.productKinds = rows;
			dispatchEvent(homeEvent);
		}
		/** 获取进货列表失败 */
		private function productKindsFaultHandler(info:Object, token:Object):void{
			
		}
			
		// ====================================================================
		public function insertOrupdateStock(stockObj:Object):void{
			
			if(stockObj.stockId == 0){
			
				insertStock(stockObj);
			}else{
				updateStock(stockObj);
			}
		}
		// ==================================== 添加新的 stockList =========================
		public function insertStock(stockObj:Object):void{
			
			var sql:String = "insert into stockList(price,totalCount,totalPrice,uint,commont,productId,dayId) values ('"+stockObj.price+"','"+stockObj.totalCount+"','"+stockObj.totalPrice+"','"+stockObj.uint+"','"+stockObj.commont+"','"+stockObj.Id+"','"+_dayId+"')";
		
			var token:MySqlToken = st.executeQuery(sql);
			
			token.addResponder(new AsyncResponder(insertStockHandler,insertStockFaultHandler,token));
		}
		private function insertStockHandler(data:Object, token:Object):void{
			
			var rs:MySqlResponse = MySqlResponse(data);
			
		}
		private function insertStockFaultHandler():void{
		}
		
		// =========================== 更新 stockList =========================================
		public function updateStock(stockObj:Object):void{
			
			var sql:String = "update stockList set price='"+stockObj.price+"',totalCount='"+stockObj.totalCount+"',uint='"+stockObj.uint+"',commont='"+stockObj.commont+"' where stockId='"+stockObj.stockId+"'";

			var token:MySqlToken = st.executeQuery(sql);
			
			token.addResponder(new AsyncResponder(updateStockHandler,updateStockFaultHandler,token));
		}
		private function updateStockHandler(data:Object, token:Object):void{
		
			var rs:MySqlResponse = MySqlResponse(data);
		}
		private function updateStockFaultHandler(data:Object, token:Object):void{
		}
		
		//----------------------------------
		// 获取上货的日期列表
		//-----------------------------------
		public function getStockDateList():void{
			
			var sql:String = "select * from years where isStock='true'";
			
			var token:MySqlToken = st.executeQuery(sql);
			
			token.addResponder(new AsyncResponder(stockListResultHandler,stockDateListFaultHandler,token));
		}
		private function stockListResultHandler(data:Object, token:Object):void{
			
			var rs:ResultSet = ResultSet(data);
			
			var rows:ArrayCollection = rs.getRows();
			
			var stockEvent:StockListEvent = new StockListEvent(StockListEvent.GETSTOCKDATELISTRESULT);
			stockEvent.dateList = rows;
			dispatchEvent(stockEvent);
		}
		private function stockDateListFaultHandler(data:Object, token:Object):void{
		}
	}
}








